/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ht.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.ht.entity.HtClient;
import com.thinkgem.jeesite.modules.ht.service.HtClientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ht.entity.HtCost;
import com.thinkgem.jeesite.modules.ht.service.HtCostService;

import java.util.List;

/**
 * 费用信息Controller
 * @author yy
 * @version 2016-12-05
 */
@Controller
@RequestMapping(value = "${adminPath}/ht/htCost")
public class HtCostController extends BaseController {

	@Autowired
	private HtCostService htCostService;

	@Autowired
	private HtClientService htClientService;

	@ModelAttribute
	public HtCost get(@RequestParam(required=false) String id) {
		HtCost entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = htCostService.get(id);
		}
		if (entity == null){
			entity = new HtCost();
		}
		return entity;
	}
	
	@RequiresPermissions("ht:htCost:view")
	@RequestMapping(value = {"list", ""})
	public String list(HtCost htCost, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HtCost> page = htCostService.findPage(new Page<HtCost>(request, response), htCost); 
		model.addAttribute("page", page);
		return "modules/ht/htCostList";
	}

	@RequiresPermissions("ht:htCost:view")
	@RequestMapping(value = "form")
	public String form(HtCost htCost, Model model) {
		model.addAttribute("htCost", htCost);
		List<HtClient> clientList =  htClientService.findList(new HtClient());
		System.out.println("clientList size :"+clientList.size());
		model.addAttribute("clientList" ,clientList );
		return "modules/ht/htCostForm";
	}

	@RequiresPermissions("ht:htCost:edit")
	@RequestMapping(value = "save")
	public String save(HtCost htCost, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, htCost)){
			return form(htCost, model);
		}
		htCostService.save(htCost);
		addMessage(redirectAttributes, "保存费用信息成功");
		return "redirect:"+Global.getAdminPath()+"/ht/htCost/?repage";
	}
	
	@RequiresPermissions("ht:htCost:edit")
	@RequestMapping(value = "delete")
	public String delete(HtCost htCost, RedirectAttributes redirectAttributes) {
		htCostService.delete(htCost);
		addMessage(redirectAttributes, "删除费用信息成功");
		return "redirect:"+Global.getAdminPath()+"/ht/htCost/?repage";
	}

}